package src.com.neusoft.nep.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.ProvidedData;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfirmAQIDateDetailsViewController {
    @FXML
    private TableView<ProvidedData> tableView;
    @FXML
    private TableColumn<ProvidedData,String> colProvince,colCity,colDate,colGridInspector,colAQILevel,colUserName;
    private ObservableList<ProvidedData> tableList = FXCollections.observableArrayList();
    private Map firstMap = new HashMap();
    @FXML
    public void initialize() {

        tableView.setItems(tableList);

        colProvince.setCellValueFactory(new PropertyValueFactory<>("province"));
        colCity.setCellValueFactory(new PropertyValueFactory<>("city"));
        colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
        colGridInspector.setCellValueFactory(new PropertyValueFactory<>("gridInspector"));
        colAQILevel.setCellValueFactory(new PropertyValueFactory<>("aqiLevel"));
        colUserName.setCellValueFactory(new PropertyValueFactory<>("userName"));

        firstMap = FileUtils.readMapObject("AdminData.txt");
        tableList.addAll(firstMap.values());
        colCity.setCellFactory(TextFieldTableCell.forTableColumn());
        colCity.setOnEditCommit(event-> {
            ProvidedData p = event.getRowValue();
            String newLocCity = event.getNewValue();
            p.setCity(newLocCity);
            firstMap.put(p.getCity(), p);
            FileUtils.writeMapObject("AdminData.txt", firstMap);
        });

    }

}

